export default {
  namespaced: true,
  state: (): RequestState => {
    return {
      timer: null,
      timeout: null,
    }
  },
  mutations: {
    /**
     * @param {number} time 请求n秒后，显示loading
     * @description: 请求n秒后，显示loading
     */
    SET_TIMER(state: RequestState, time: number) {
      if (state.timer) {
        clearTimeout(state.timer)
        state.timer = null
      }

      state.timer = setTimeout(() => {
        uni.showLoading({ title: '全力加载中...' })
        state.timer = null
        state.timeout = setTimeout(() => {
          uni.hideLoading()
          uni.$u.toast('网络不给力，请稍后再试')
          clearTimeout(state.timer as NodeJS.Timeout)
          state.timer = null
          state.timeout = null
        }, 30000)
      }, time * 1000)
    },

    REMOVE_TIMER(state: RequestState) {
      if (state.timer) {
        clearTimeout(state.timer)
        state.timer = null
      }
      if (state.timeout) {
        clearTimeout(state.timeout)
        state.timeout = null
      }
      uni.hideLoading()
    },
  },
  actions: {},
}
